package com.press.controllers.blog;

import com.press.bean.ContentBean;
import com.press.bean.MappingBean;
import com.press.beanforview.VContentBean;
import com.press.beanforview.VMenuBean;
import com.press.beanforview.VPageBean;
import com.press.controllers.admin.PersonalDataController;
import com.press.services.ArticalTaxService;
import com.press.services.ContentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller
@RequestMapping("/blog")
public class IndexController {
    public static int pageNow=1;
    public static int pageCount;
    private static Logger log = Logger.getLogger(IndexController.class);
    @Resource(name="ArticalTaxService")
    private ArticalTaxService articalTaxService;
    @Resource(name="contentService")
    private ContentService contentService;

    public ArticalTaxService getArticalTaxService() {
        return articalTaxService;
    }

    public void setArticalTaxService(ArticalTaxService articalTaxService) {
        this.articalTaxService = articalTaxService;
    }

    public ContentService getContentService() {
        return contentService;
    }

    public void setContentService(ContentService contentService) {
        this.contentService = contentService;
    }

    @RequestMapping("/index")
    public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取总共页数
        String NoBinContentCount = contentService.getNoBinCount();
        pageCount = (int)Math.ceil(Integer.parseInt(NoBinContentCount)/6.0);

        UtilController utilController = (UtilController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("utilController");
        //存入推荐文章，top10文章，菜单选项
        utilController.contentsRecommend(request);
        utilController.contentsTop10(request);
        utilController.menus(request);
        //处理分页请求参数pn
        handlePage(request);
        //存入pages，pageCount，pageNow属性
        getPage(request);
        //获取文章列表
        getContents(request);
        //页面跳转处理
        Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");
        request.setAttribute("title",option.get("web_name")+",欢迎访问！");
        String forwardUrl = "/"+option.get("web_template_id")+"/index.jsp";
        request.getRequestDispatcher(forwardUrl).forward(request,response);
    }
    public void getContents(HttpServletRequest request){
        //从数据库获取文章列表
        List<ContentBean> contentlist = articalTaxService.getContentList(pageNow);
        //声明展示vcontentList
        List<VContentBean> vContentList = new ArrayList<>();
        //声明两个字符串，用于构造查看文章的url和请求分类的url
        String urlContent = request.getServletContext().getAttribute("URLContent")+"?content_id=";
        String urlTax = request.getServletContext().getAttribute("URLTaxonomy")+"?tax=";
        //遍历查询到的文章，对vcontentlist进行赋值
        for(ContentBean content : contentlist){
            VContentBean vContent = new VContentBean();
            //对vcontent中的分类map进行赋值
            for(MappingBean mapping : contentService.getmapping()){
                if(mapping.getContent_id().equals(content.getContent_id())){
                    log.info(mapping.getaTaxonomy().getTaxonomy_id());
                    vContent.getTaxonomys().put(urlTax+mapping.getaTaxonomy().getTaxonomy_id(),mapping.getaTaxonomy().getTaxonomy_title());
                }
            }
            //将content中的值进行转移
            vContent.setTitle(content.getContent_title());
            vContent.setUrl(urlContent+content.getContent_id());
            vContent.setAuthor(content.getContent_author());
            vContent.setCreateDate(content.getContent_created());
            vContent.setModifiedDate(content.getContent_modified());
            vContent.setSummary(content.getContent_summary());
            vContent.setThumbnail(request.getContextPath()+content.getContent_thumbnail());
            vContent.setWatched(content.getContent_watched());
            //将vcontent存入vconentlist中
            vContentList.add(vContent);
        }
        //将vcontentlist存入request中
        request.setAttribute("contents",vContentList);
    }
    public void handlePage(HttpServletRequest request){
        String pn = request.getParameter("pn");
        if(pn==null){
            pageNow=1;
        }else if(pn.equals("next")){
            if(pageNow<pageCount){
                pageNow++;
            }
        }else if(pn.equals("last")){
            if(pageNow>1){
                pageNow--;
            }
        }else {
            pageNow=Integer.parseInt(pn);
        }
    }
    private void getPage(HttpServletRequest request){
        List<VPageBean> vPages = new ArrayList<>();

        String urlPage = request.getServletContext().getAttribute("URLIndex")+"?pn=";

        VPageBean last = new VPageBean();
        last.setStatus("");
        last.setNum("<<");
        last.setUrl(urlPage+"last");
        vPages.add(last);

        for(int i = 1;i<=pageCount;i++){
            VPageBean vPage = new VPageBean();
            vPage.setNum(String.valueOf(i));
            vPage.setUrl(urlPage+i);
            if(i==pageNow){
                vPage.setStatus("active");
            }else {
                vPage.setStatus("");
            }
            vPages.add(vPage);
        }

        VPageBean next = new VPageBean();
        next.setStatus("");
        next.setNum(">>");
        next.setUrl(urlPage+"next");
        vPages.add(next);

        request.setAttribute("pages",vPages);

        request.setAttribute("pageCount",pageCount);

        request.setAttribute("pageNow",pageNow);
    }
}
